<?php
include('./db_config.php');
session_start();
$link = mysql_connect($db_host, $db_user, $db_password) or die ("Connection failed due to: " . mysql_error());
mysql_select_db ($db_name) or die("Selection failed due to: " . mysql_error());
if($_SESSION["valid"] != "true") {
	header("Location: ./login.php");
	exit;
}
elseif ((empty($_POST["vLocation"])) || (empty($_POST["vCategory"]))){
	header("Location: ./choose.php?missing=true");
	exit;
}
require("./resource.php");
top("Your search results");
$search = $_POST["search_str"];
$loc_arr = $_POST["vLocation"];
$cat_arr = $_POST["vCategory"];
$location = implode(" ", $loc_arr);
$category = implode(" ", $cat_arr);
if (empty($_POST["search_str"])) {
	$query = "SELECT *
             FROM employment
             WHERE `fk_category` LIKE '". $category ."' AND`fk_location` LIKE('". $location ."')";
}
else {
	$query = "SELECT *
             FROM employment
             WHERE `description` LIKE '%".  $search ."%'";
}
//$query = "SELECT * FROM employment"; -used for testing
$result = mysql_query($query) or die("Query failed due to: " . mysql_error());
echo "<table align='center'><tr bgcolor='grey' style='color:white'><td>Date</td><td>Title</td><td>Company</td><td>Location</td></tr>";
while ($row = mysql_fetch_assoc($result)) {
	$location = explode(" ", $location); // now turned into an array
	$count = 0;
	foreach ($location as $value) {
		$count++;
	}
	$location = implode(" ", $location);
	if ($count < 2){
		$query2 = "SELECT name FROM location WHERE pk='" . $location . "'";
		//whorable style for any large scale site since multiple queries put the server under too much load
		$result2 = mysql_query($query2) or die("Query failed due to: " . mysql_error());
		$row2 = mysql_fetch_assoc($result2);
		$loc = $row2['name'];
	}
	else{
		$loc = 'MANY';
	}
	$account = $row['fk_account'];
	$query3= "SELECT name FROM account WHERE pk='" . $account . "'";
	$result3 = mysql_query($query3) or die("Query failed due to: " . mysql_error());
	$row3 = mysql_fetch_assoc($result3);
	$acc = $row3['name'];
	echo "<tr bgcolor='white'><td>" . $row['date'] . "</td><td><a href=./description.php?pkref=" . $row['pk'] . ">" . $row['title'] . "</a>"  . "</td><td>" . $acc . "</td><td>" . $loc ."</td></tr>";
}
echo "</table>";
echo "<a href='./choose.php?show=search'>Return Home</a>";
bottom();
?>